package com.example.myview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;

import java.util.Calendar;

public class MyView extends View {
    Paint paintCirCle;
    Paint paintLine;
    Paint paintNumber;
    Paint paintSecond;
    Paint paintMniutes;
    Paint paintHour;


    int width;
    int height;

    Calendar calendar;
    public static final int RefreshUI=0*23;
    // 每隔一秒 刷新一次UI
    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case RefreshUI:
                    calendar = Calendar.getInstance();
                    invalidate();
                    // 自己 给自己发消息
                    handler.sendEmptyMessageDelayed(RefreshUI,1000);
                    break;
                default:
            }
        }
    };
    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        calendar=Calendar.getInstance();
        initPaint();
        handler.sendEmptyMessage(RefreshUI);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //画圆盘
        canvas.drawCircle(width/2,height/2,350,paintCirCle);

        for (int i = 1; i <=12 ; i++) {
            canvas.save();
            //旋转画布30*i度
            canvas.rotate(360/12*i,width/2,height/2);
            //画线
            canvas.drawLine(width/2,height/2-325,width/2,height/2-355,paintLine);
            //画数字
            canvas.drawText(""+i,width/2,height/2-290,paintNumber);
            //恢复
            canvas.restore();
        }

        //画分针的刻度 60个
        for (int i = 1; i <=60 ; i++) {
            if(i%5==0){
                continue;
            }
            canvas.save();
            canvas.rotate(360/60*i,width/2,height/2);
            canvas.drawLine(width/2,height/2-335,width/2,height/2-350,paintLine);
            canvas.restore();
        }
        //获取的分秒
        int second = calendar.get(Calendar.SECOND);
        int minute = calendar.get(Calendar.MINUTE);
        int hour=calendar.get(Calendar.HOUR);


        //画秒针
        canvas.save();
        float second_Degree = second / 60f * 360;
        canvas.rotate(second_Degree,width/2,height/2);
        canvas.drawLine(width/2,height/2+40,width/2,height/2-280,paintSecond);
        canvas.restore();

        //画分针
        canvas.save();
        float minutes_Degree = minute / 60f * 360;
        canvas.rotate(minutes_Degree,width/2,height/2);
        canvas.drawLine(width/2,height/2+10,width/2,height/2-230,paintMniutes);
        canvas.restore();
        //画时针
        canvas.save();
        float Hour_Degree = hour / 12f * 360;
        canvas.rotate(Hour_Degree,width/2,height/2);
        canvas.drawLine(width/2,height/2,width/2,height/2-180,paintHour);
        canvas.restore();

    }

    private void initPaint() {

        // 初始化 圆盘的 画笔
        paintCirCle=new Paint();
        paintCirCle.setColor(Color.BLACK);
        paintCirCle.setStrokeWidth(8);
        paintCirCle.setStyle(Paint.Style.STROKE);
        paintCirCle.setAntiAlias(true);

        // 初始化 线
        paintLine=new Paint();
        paintLine.setColor(Color.BLACK);
        paintLine.setStrokeWidth(10);
        paintLine.setStyle(Paint.Style.STROKE);
        paintLine.setAntiAlias(true);
        // 初始化 时钟数字
        paintNumber=new Paint();
        paintNumber.setColor(Color.GREEN);
        paintNumber.setTextAlign(Paint.Align.CENTER);
        paintNumber.setTextSize(35);
        paintNumber.setAntiAlias(true);

        // 初始化 秒
        paintSecond=new Paint();
        paintSecond.setColor(Color.BLUE);
        paintSecond.setStrokeWidth(10);
        paintSecond.setStyle(Paint.Style.FILL);
        paintSecond.setAntiAlias(true);

        // 初始化 分
        paintMniutes=new Paint();
        paintMniutes.setColor(Color.RED);
        paintMniutes.setStyle(Paint.Style.FILL);
        paintMniutes.setStrokeWidth(15);
        paintMniutes.setAntiAlias(true);

        //初始化 时
        paintHour=new Paint();
        paintHour.setColor(Color.YELLOW);
        paintHour.setStyle(Paint.Style.FILL);
        paintHour.setStrokeWidth(15);
        paintHour.setAntiAlias(true);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        width=getDefaultSize(getSuggestedMinimumWidth(),widthMeasureSpec);
        height=getDefaultSize(getSuggestedMinimumWidth(),heightMeasureSpec);
        setMeasuredDimension(width,height);
    }

}
